/*
 * The Yard Utilties - http://www.theyard.net/
 * 
 * Copyright (c) 2008 by Vlideshow, Inc..  All Rights Resrved.
 * 
 * This library is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU Lesser General Public License as published by the Free Software 
 * Foundation; either version 2.1 of the License, or (at your option) any later 
 * version. 
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along 
 * with this library; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package net.theyard.components.netconnectionsm
{
  import net.theyard.components.Utils;
  import flash.net.NetConnection;

  /**
   * This class represents the NetConnectionStateMachine#STATE_DISCONNECTING
   * state and implements all transitions.
   *
   * @see ConnectionState
   * @see NetConnectionStateMachine#STATE_DISCONNECTING
   */
  internal class DisconnectingState implements ConnectionState
  {
    private var sm:NetConnectionStateMachine;

    public function DisconnectingState(sm:NetConnectionStateMachine)
    {
      Utils.ytrace("Starting state: " + this.toString());;
      this.sm = sm;
    }

    public function toString():String
    {
      return NetConnectionStateMachine.STATE_DISCONNECTING;
    }

    public function connect(nc:NetConnection, command:String, ...args):void
    {
      Utils.ytrace("Waiting to disconnect from old uri: " + sm.uri + "; ignoring new request to connect");
    }
    public function network_connected(nc:NetConnection):void
    {
      Utils.ytrace("Disconnecting from URI but got unexpected connection event.  Throwing an error: " + sm.uri);
      throw new Error("Got unexpected connected event");
    }
    public function disconnect(nc:NetConnection):void
    {
      Utils.ytrace("Got additional request to disconnect; ignoring and waiting for first request to finish");
    }

    public function network_disconnected(nc:NetConnection):void
    {
      Utils.ytrace("Successfully disconnected from: " + sm.uri);
      sm.setUri(null);
      sm.gotoState(NetConnectionStateMachine.STATE_DISCONNECTED);
    }
  }
}
